/*!
 * Page: mine/topics/Topics
 * License: MIT
 * Created: 2019-09-16 16:16
 */

// /* eslint-disable @typescript-eslint/no-object-literal-type-assertion */
import { topicApi } from '/api/topic'
import { getServer } from '/store/mutations'
import { GlobalData } from '/store/state'
import { $event } from '/utils'
Page({
  data: {
    topics: [] as DomoTopic[],
    type: 'created',
    query: {
      limit: 15,
      index: 0,
      for_user: '',
      after: 0,
    },
    canLoad: true,
    loading: false,
    user: {} as DomoUser,
    paytoast: false,
    topic: {} as DomoTopic,
    currencyName: GlobalData.currencyName,
  },
  onPullDownRefresh() {
    this.setData(
      {
        query: {
          ...this.data.query,
          index: 0,
          after: 0,
        },
      },
      () => {
        this.fetchTopics()
      },
    )
  },
  onReachBottom() {
    this.fetchTopics(true)
  },
  onLoad(query: any) {
    const user = wx.getStorageSync(`${getServer()}/logged`).data || {}
    this.setData(
      {
        type: query.type || 'created',
        query: {
          ...this.data.query,
          for_user: query.user ? query.user : '',
        },
        user,
        currencyName: GlobalData.currencyName,
      },
      () => {
        wx.setNavigationBarTitle({
          title: `${query.type === 'joined' ? '加入的' : query.user == user.id ? '我的' : 'Ta的'
            }圈子`,
        })
        this.fetchTopics()
      },
    )
  },
  onShow() {
    $event.on('after:joinTopic', this, (topic: DomoTopic) => {
      this.changeTopic(topic)
    })
    $event.on('after:deleteTopic', this, (topic: DomoTopic) => {
      this.deleteTopic(topic)
    })
    $event.on('erorr:integral', this, ({ paytoast, topic }) => {
      this.setData({
        paytoast,
        topic,
      })
    })
  },
  /**
   * 获取话题列表
   * @param loadMore
   */
  async fetchTopics(loadMore = false) {
    // eslint-disable-next-line @typescript-eslint/no-this-alias
    const {
      data: { query, canLoad, loading, type },
    } = this
    if (loading) return
    if (loadMore) {
      if (!canLoad) return
    }
    this.setData({
      loading: true,
    })
    const method = type === 'created' ? 'getUserTopics' : 'getFollowedTopics'
    const topics = await topicApi[method](
      type === 'created' ? { ...query, status: 'all' } : (query as any),
    )
    this.setData(
      {
        topics: loadMore ? [...this.data.topics, ...topics] : topics,
        query: {
          ...this.data.query,
          after: topics[topics.length - 1] ? topics[topics.length - 1].id : 0,
          index: topics[topics.length - 1] ? topics[topics.length - 1].id : 0,
        },
        canLoad: topics.length === this.data.query.limit,
        loading: false,
      },
      () => {
        !loadMore && wx.stopPullDownRefresh()
      },
    )
  },
  /** 缓存改变状态 */
  changeTopic(topic: DomoTopic) {
    const topics = this.data.topics
    topics[topics.findIndex((i: any) => i.id === topic.id)] = topic
    this.setData({
      topics,
    })
  },
  /** 删除圈子时改变缓存 */
  deleteTopic(topic: DomoTopic) {
    const topics = this.data.topics
    topics.splice(
      topics.findIndex((i: any) => i.id === topic.id),
      1,
    )
    this.setData({
      topics,
    })
  },
})
